package demo.service;

import demo.dao.CommonDao;

import demo.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.Date;
import java.util.List;

@Service
public class CommonService {

    @Autowired
    private CommonDao commonDao;
//    Date date = new Date();
//    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd ");
//    SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
//    SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//    String dateStr = dateFormat.format(date);
//    String timeStr = timeFormat.format(date);
//    String dateTime = dateTimeFormat.format(date);
    public record sign_on_work(int emp_id){  //上岗签到
        employees emp=commonDao.getInfo(emp_id);
        Date date = new Date();
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        String timeStr = timeFormat.format(date);
        if(timeStr.compareTo(emp.getArrival_time())>0) {  //迟到
            emp.setMonthly_salary(emp.getMonthly_salary()-100);
            System.out.println("-100!!");
            return commonDao.Reducer(emp);
        }
        else return commonDao.Commonreturn(emp);
    }
    public record sign_off_work(int emp_id){    //下岗签到
        employees emp=commonDao.getInfo(emp_id);
        Date date = new Date();
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        String timeStr = timeFormat.format(date);
        if(timeStr.compareTo(emp.getDeparture_time())<0) {  //早退
            emp.setMonthly_salary(emp.getMonthly_salary()-100);
            return commonDao.OffWorkReducer(emp);
        }
        else return commonDao.OffWorkCommonReturn(emp);
    }
    public List<record> queryRecord(int emp_id) {
        return commonDao.queryRecord(emp_id);
    }
    public employees getInfo(int emp_id){ return commonDao.getInfo(emp_id);}
    public managers addManagers(managers man) {
        employees emp=commonDao.getInfo(man.getEmp_id());
        int restHoliday=emp.getRest_holiday();
        LocalDate start = LocalDate.parse(man.getStart_date());
        LocalDate end = LocalDate.parse(man.getEnd_date());
        Period p = Period.between(start,end);
        int interval = 365 * p.getYears() + 30 * p.getMonths() + p.getDays();
        if(restHoliday<interval){return commonDao.worning();} //请假天数超过剩余假期
        return commonDao.addManagers(man);
    }
    public List<managers> queryManagers(int emp_id) {
        return commonDao.queryManagers(emp_id);
    }
//    private StudentDao studentDao;
//
//    public List<Student> queryAllStudents() {
//        return studentDao.queryAllStudents();
//    }
//
//    public Student queryStudentById(int id) {
//        return studentDao.queryStudentById(id);
//    }
//
//    public Student insertStudent(Student student) {
//        return studentDao.insertStudent(student);
//    }
//
//    public Student updateStudentById(int id, Student student) {
//        return studentDao.updateStudentById(id, student);
//    }
//
//    public void deleteStudentById(int id) {
//        studentDao.deleteStudentById(id);
//    }
}
